<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 银行管理类
 * @author hmw
 *
 */
class Admin_bank extends MY_Controller {

	function Admin_bank()
	{
		parent::MY_Controller();
        parent::check_admin_login();
	}
	
	function index(){
		self::bank_list();
	}
	
	/**
	 * 银行列表
	 * @return null
	 */
	function bank_list(){
		$this->load->model('mbank');
		$banks = $this->mbank->get_bank_list();
		$this->template->view('admin/bank_list',array('banks'=>$banks));
	}
	
	/**
	 * 显示添加银行表单
	 * @return null
	 */
	function bank_new(){
		$this->template->view('admin/bank_add');
	}
	
	/**
	 * 添加银行
	 * @return null
	 */
	function bank_add(){
		$config['upload_path'] = $this->config->item('upload_uri').'bank_img/';
		if(!file_exists($config['upload_path'])){
			mkdir($config['upload_path']);
			chmod($config['upload_path'],0777);
		}
		$config['allowed_types'] = $this->config->item('bank_img_types');
		$config['max_size'] = $this->config->item('bank_img_max_size');
		$config['max_width'] = $this->config->item('bank_img_width');
		$config['max_height'] = $this->config->item('bank_img_height');
		$this->load->library('upload', $config);
		if (!$this->upload->do_upload('bank_logo')) {
			echo '文件上传失败，可能原因为：<br />'.$this->uplaod->display_errors();
		} else {
			$bank_name = $this->input->post('bank_name') ? $this->input->post('bank_name') : '未知银行';
			$account  = $this->input->post('account');
			if(!$account){
				echo '银行帐号必须填写！';exit;
			}
			$owner = $this->input->post('owner');
			$state = $this->input->post('state');
			$bank_link = $this->input->post('bank_link');
			$border = $this->input->post('border') ? $this->input->post('border') : 1;
			$data = $this->upload->data();
			$bank_logo = $data['file_name'];
			$values = array('bank_name'=>$bank_name,'account'=>$account,'state'=>$state,'bank_logo'=>$bank_logo,'border'=>$border,'owner'=>$owner,'bank_link'=>$bank_link);
			$this->load->model('mbank');
			if ($this->mbank->insert_new_bank($values)) {
				echo '银行添加成功！&nbsp;&nbsp;<a href="/admin_bank/bank_new/">继续添加</a>&nbsp;&nbsp;<a href="/admin_bank/bank_list/">查看列表</a>';
			} else {
				echo '银行添加失败！&nbsp;&nbsp;<script type="text/javascript"><a href="javascript:window.history.back();">返回</a></script>';
			}
		}
	}
	
	/**
	 * 禁用银行
	 * @param $id
	 * @return unknown_type
	 */
	function bank_disable($id=''){
		if ($id === '') {
			echo '必须选择项目方可继续操作！';return;
		}
		$this->load->model('mbank');
		if ($this->mbank->update_bank_state($id,'0')) {
			echo '所选银行成功停用！';
		} else {
			echo '停用失败！';
		}
	}
	
	/**
	 * 启用银行
	 * @param $id
	 * @return unknown_type
	 */
	function bank_enable($id=''){
		if ($id === '') {
			echo '必须选择项目方可继续操作！';return;
		}
		$this->load->model('mbank');
		if ($this->mbank->update_bank_state($id,'1')) {
			echo '所选银行成功启用！';
		} else {
			echo '启用失败！';
		}
	}
	
	/**
	 * 删除银行
	 * @param $id
	 * @return unknown_type
	 */
	function bank_del($id){
		if ($id === '') {
			echo '必须选择项目方可继续操作！';return;
		}
		$this->load->model('mbank');
		if ($this->mbank->delete_bank($id)) {
			echo '删除成功！';
		} else {
			echo '删除失败！';
		}
	}
	
	/**
	 * 修改银行信息表单页面
	 * @param $id
	 * @return unknown_type
	 */
	function bank_edit($id){
		if ($id === '') {
			echo '必须选择项目方可继续操作！';return;
		}
		$this->load->model('mbank');
		$info = $this->mbank->get_bank_info($id);
		//print_r($info);exit;
		$this->template->view('admin/bank_edit',array('info'=>$info));
	}
	
	/**
	 * 处理修改银行信息
	 * @return null
	 */
	function bank_modify(){
		$id = $this->input->post('id');
		$bank_name = $this->input->post('bank_name');
		$account  = $this->input->post('account');
		$state = $this->input->post('state');
		$owner = $this->input->post('owner');
		if(!$owner || !$account){
			echo '你有必填信息没有填写！';exit;
		}
		$bank_link = $this->input->post('bank_link');
		$border = $this->input->post('border') ? $this->input->post('border') : 1;
		$values = array('bank_name'=>$bank_name,'account'=>$account,'state'=>$state,'border'=>$border,'owner'=>$owner,'bank_link'=>$bank_link);
		
		$config['upload_path'] = $this->config->item('upload_uri').'bank_img/';
		$config['allowed_types'] = $this->config->item('bank_img_types');
		$config['max_size'] = $this->config->item('bank_img_max_size');
		$config['max_width'] = $this->config->item('bank_img_width');
		$config['max_height'] = $this->config->item('bank_img_height');
		$this->load->library('upload', $config);
		if (!$this->upload->do_upload('img')) {
			//echo '文件上传失败，可能原因为：<br />'.$this->uplaod->display_errors();
		} else {
			$data = $this->upload->data();
			$img = $data['file_name'];
			$values['bank_logo'] = $img;
		}
		$this->load->model('mbank');
		if ($this->mbank->update_bank_info($id,$values)) {
			echo '银行修改成功！&nbsp;&nbsp;<a href="/admin_bank/bank_list/">查看列表</a>';
		} else {
			echo '银行修改失败！ &nbsp;&nbsp;<script type="text/javascript"><a href="javascript:window.history.back();">返回</a></script>';
		}
	}
}
?>
